#include <minix/drivers.h>
#include <minix/vtreefs.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>


static void my_init_hook(void)
{       
    /* This hook will be called once, after VTreeFS has initialized. */
    struct inode_stat dir_stat, file_stat;
    struct inode *file_inode, *dir_inode;

    // Setup the directory
    dir_stat.mode = S_IFDIR | 0555; // Read and execute (open) for everybody
    dir_stat.uid = 0;
    dir_stat.gid = 0;
    dir_stat.size = 0;
    dir_stat.dev = NO_DEV;
    
    // Setup the file
    file_stat.mode = S_IFREG | 0444; // Ready only for everybody
    file_stat.uid = 0;
    file_stat.gid = 0;
    file_stat.size = 0;
    file_stat.dev = NO_DEV;
    
    // Add the directory to the root
    dir_inode = add_inode(get_root_inode(), "as3", NO_INDEX, &dir_stat, 0, (cbdata_t) 1);
    // Add the file to the directory
    file_inode = add_inode(dir_inode, "t1", NO_INDEX, &file_stat, 0, (cbdata_t) 2);
}

static int my_read_hook(struct inode *inode, off_t offset, char **ptr, size_t *len, cbdata_t cbdata){
        /* This hook will be called every time a regular file is read. We use
         * it to dyanmically generate the contents of our file.
         */
        static char data[33];
        const char *str;
        
        // The content of the page
        str = "Hello world! Group 19 was here.\n";
        // Copy the string
        strcpy(data, str);
        
       /* If the offset is beyond the end of the string, return EOF. (len=0 AND return OK) */
        if (offset > strlen(data)) {
                *len = 0;

                return OK;
        }
        
        

        /* Otherwise, return a pointer into 'data'. If necessary, bound the
         * returned length to the length of the rest of the string. Note that
         * 'data' has to be static, because it will be used after this function
         * returns.
         */
        *ptr = data + offset;

        if (*len > strlen(data) - offset)
                *len = strlen(data) - offset;

        return OK;
}

/* The table with callback hooks. */
struct fs_hooks my_hooks = {
        my_init_hook,
        NULL, /* cleanup_hook */
        NULL, /* lookup_hook */
        NULL, /* getdents_hook */
        my_read_hook,
        NULL, /* rdlink_hook */
        NULL  /* message_hook */
};

int main(void){
    struct inode_stat root_stat;

    /* Fill in the details to be used for the root inode. It will be a
     * directory, readable and searchable by anyone, and owned by root.
     */
    root_stat.mode = S_IFDIR | 0555; // Read and execute (open) for everybody
    root_stat.uid = 0;
    root_stat.gid = 0;
    root_stat.size = 0;
    root_stat.dev = NO_DEV;

    /* Now start VTreeFS. Preallocate 10 inodes, which is more than we'll
     * need for this example. No indexed entries are used.
     */
    start_vtreefs(&my_hooks, 10, &root_stat, 0);

    return 0;
}